from gyroScope import accData
from networkLib import client
from gameLib import gameScene, gameObj, gameControl
from machine import I2C, SPI, Pin, Timer
import usocket as socket
import network, st7789, icm20948, utime

class gameInfoVar(object):
    def __init__(self):
        self.player = ''
        self.colidMark = 0
        self.colidShow = 0    

class DataPool(object):
    def __init__(self):
        self.localAcclist = [0, 0]
        self.remoteAcclist = [0.0, 0.0]

def infoCtrlFunc(args):
    if gi.colidMark == 1:
        gi.colidShow = 1
    else:
        gi.colidShow = 0

def gameInfo(display, tag):
    if tag == 2 or tag == 3:
        if tag == 2:
            str1 = 'BLUE WIN!'
        if tag == 3:
            str1 = 'WHITE WIN!'
        display.draw_string(60, 120, str1, color = st7789.WHITE, bg = st7789.BLACK, size = 3)
        utime.sleep(1)
        display.draw_string(60, 120, str1, color = st7789.BLACK, bg = st7789.BLACK, size = 3)
        display.draw_string(60, 120, 'Game Over!', color = st7789.WHITE, bg = st7789.BLACK, size = 3)
        utime.sleep_ms(500)
        display.draw_string(60, 120, 'Game Over!', color = st7789.BLACK, bg = st7789.BLACK, size = 3)
        display.draw_string(40, 120, 'Restarting...', color = st7789.WHITE, bg = st7789.BLACK, size = 3)
        utime.sleep_ms(500)
        display.draw_string(40, 120, 'Restarting...', color = st7789.BLACK, bg = st7789.BLACK, size = 3)
    if tag == 1:
        display.draw_string(60, 120, 'Colid!', color = st7789.WHITE, bg = st7789.BLACK, size = 3)
        gi.colidMark = 0
        if gi.colidShow == 0:
            display.draw_string(60, 120, 'Colid!', color = st7789.BLACK, bg = st7789.BLACK, size = 3)

if __name__ == '__main__':
    # init network
    SSID = 'TEST_Wafflenano'
    PASSWORD = '1234567890'
    PORT = 5000
    # select instance_mode as sta
    sta = network.WLAN(network.STA_IF)
    client = client(sta, SSID, PASSWORD, PORT)

    # init gyroScope
    imu = icm20948.ICM20948(I2C(0), gyro_scale=icm20948.DPS_2000, accel_scale = icm20948.GPM_8)
    accdata = accData(imu)

    # init Screen
    spi = SPI(0, baudrate=40000000, polarity=1, phase=0, bits=8, endia=0, sck=Pin(6), mosi=Pin(8))
    display = st7789.ST7789(spi, 240, 240, reset=Pin(11,func=Pin.GPIO, dir=Pin.OUT), dc=Pin(7,func=Pin.GPIO, dir=Pin.OUT))
    
    # init game
    gi = gameInfoVar()
    gi.player = 'player2'
    scene = gameScene(display, st7789.BLACK)
    scene.Scene(display)
    p1 = gameObj(60, 120)
    p2 = gameObj(180, 120)
    playerList = [p1, p2]
    gc = gameControl(playerList)
    # init dataPool
    dp = DataPool()
    dp.localAcclist = accdata.handleAccData()

    # game control
    gameThread = Timer(0)
    gameThread.init(type = Timer.PERIODIC, callback = infoCtrlFunc, ticks = 1000)

    # Main thread:
    # Control network and gyroScope
    cnt = 0
    while cnt < 1000:
        scene.Scene(display)
        # network handle
        dp.localAcclist = accdata.handleAccData()
        dp.remoteAcclist[0] = client.run(dp.localAcclist[0], dp.remoteAcclist[0])
        dp.remoteAcclist[1] = client.run(dp.localAcclist[1], dp.remoteAcclist[1])
        # test
        print('local: acc_x = {}, acc_y = {}'.format(dp.localAcclist[0], dp.localAcclist[1]))
        print('remote: acc_x = {}, acc_y = {}'.format(dp.remoteAcclist[0], dp.remoteAcclist[1]))
        # game handle
        endmark = gc.run(display, playerList, dp.localAcclist, dp.remoteAcclist, gi.player)
        print('endmark = {}'.format(endmark))
        if endmark == 2 or endmark == 3:
            gc.init(display, playerList)
            gameInfo(display, endmark)
        elif endmark == 1:
            gameInfo(display, endmark)
        utime.sleep_ms(10)
        cnt += 1
    display.draw_string(60, 120, 'OUT TIME!', color = st7789.WHITE, bg = st7789.BLACK, size = 3)